import React, {useEffect} from "react";
import {Breadcrumb, Button, Col, Form, Input, message, Row} from "antd";
import {useNavigate} from "react-router-dom";
import type {Result} from "@/types/result";
import recruiterApi from "@/api/recruiterApi.ts";
import {Recruiter} from "@/types/resp/recruiter";


const RecruiterAddOfHr: React.FC = () => {

  const navigate = useNavigate();
  const [form] = Form.useForm<Recruiter>();
  const [messageApi, messageContextHolder] = message.useMessage();

  const rules = {
    enterpriseName: [
      { required: true, message: '请输入企业名称' }
    ],
    username: [
      { required: true, message: '请输入用户名' }
    ],
    name: [
      { required: true, message: '请输入姓名' }
    ],
    password: [
      { required: true, message: '请输入密码' }
    ],
    contactPhone: [
      { required: true, message: '请输入联系电话' }
    ],
    email: [
      { required: true, message: '请输入邮箱' }
    ],
  };

  let recruiterForm: Partial<Recruiter> = {}

  const handleClick = (path: string) => {
    navigate(path);
  };

  const breadcrumbsItems = [
    {title: '首页', className: 'breadcrumbHome', onClick: () => handleClick('/Home')},
    {title: '编辑招聘专员信息'}
  ]

  useEffect(() => {
    initRecruiterFormById(1)
  }, []);

  const initRecruiterFormById = async (id: number) => {
    const resp: Result<Recruiter> = await recruiterApi.findById(id)
    if (resp && resp.code === 1 && resp.data) {
      recruiterForm = resp.data
      form.setFieldsValue(resp.data);
    }
  }

  const onSave = async () => {
    try {
      await form.validateFields()

      let recruiter2Save:Recruiter = form.getFieldsValue()
      recruiter2Save = Object.assign(recruiterForm, recruiter2Save)
      const resp: Result<void> = await recruiterApi.save(recruiter2Save);
      if (!resp || resp.code !== 1) {
        messageApi.error('保存失败:' + (resp.msg ? resp.msg : '未知异常'));
        return
      }
      messageApi.success('保存成功');
    } catch (error) {
      console.error('There was an error submitting the form!', error);
    }
  };

  return (
    <div>
      {messageContextHolder}
      <Breadcrumb items={breadcrumbsItems} style={{marginBottom: 'var(--container-margin)'}} />
      <Form form={form} name="recruiterForm" labelCol={{span: 6}}>
        <Row gutter={10}>
          <Col span={11}>
            <Form.Item name="enterpriseName" label="企业名称"  rules={rules.enterpriseName}>
              <Input disabled />
            </Form.Item>
          </Col>
          <Col span={11}>
            <Form.Item name="username" label="用户名" rules={rules.username}>
              <Input disabled/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={10}>
          <Col span={11}>
            <Form.Item name="name" label="姓名" rules={rules.name}>
              <Input maxLength={64} placeholder="请输入姓名"/>
            </Form.Item>
          </Col>
          <Col span={11}>
            <Form.Item name="password" label="密码" rules={rules.password}>
              <Input maxLength={255} placeholder="请输入密码"/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={10}>
          <Col span={11}>
            <Form.Item name="contactPhone" label="联系电话" rules={rules.contactPhone}>
              <Input maxLength={64} placeholder="请输入联系电话"/>
            </Form.Item>
          </Col>
          <Col span={11}>
            <Form.Item name="email" label="邮箱" rules={rules.email}>
              <Input maxLength={64} placeholder="请输入邮箱"/>
            </Form.Item>
          </Col>
        </Row>
      </Form>
      <div className="custom-footer">
        <Button type="primary" onClick={onSave}>保存</Button>
      </div>
    </div>
  )
}

export default RecruiterAddOfHr;
